<style scoped lang="scss">
	.page {
		// height: 100vh;
		overflow: hidden;
	}

	.down-app-main {
		background: url('@/common/images/downApp/downAppBg.jpg') no-repeat;
		background-size: 100% 100%;
		height: 95vh;
		overflow: hidden;
	}

	.down-app-main-content {
		/* padding-top: 380px; */
		padding-top: 40px;
	}

	.down-app-main-img {
		margin: auto;
		width: R(250);
		height: R(250);
		padding-bottom: R(150);
	}

	.down-app-main-btn {
		/* display: flex;
  align-items: center;
  text-align: center; */
	}

	.down-app-main-btn-left {
		flex: 1;
		display: flex;
		justify-content: center;
		margin-bottom: R(30);
		/*margin: 0 0 0 .5rem;*/
	}

	.down-app-main-btn-right {
		display: flex;
		justify-content: center;
		/* margin: 0 0.5rem 0 0; */
	}

	.down-app-main-btn /deep/ button {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 30px;
		width: 150px;
	}

	.down-app-main-btn /deep/ .button__icon {
		display: flex;
	}

	.weixinTip {
		position: fixed;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		z-index: 1000;
		background-color: rgba(0, 0, 0, 0.8);
		filter: alpha(opacity=80);
	}

	.weixinTip p {
		text-align: center;
		margin-top: 10%;
		padding-left: 5%;
		padding-right: 5%;
	}

	.weixinTip image {
		max-width: 100%;
		width: 100%;
		height: 500upx;
	}

	.imgBox {
		margin-top: R(50);
	}

	.img,
	.img2 {
		display: block;
		width: 60%;
		margin: 0 auto R(10);
	}

	.img {
		height: R(300);
	}

	.img2 {
		height: R(50);
	}

	.c-btn {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 30px;
		width: 150px;
		background: #fff;
		border-radius: R(40);

		.icon {
			width: R(32);
			height: R(36);
			margin-right: R(12);
		}
	}
</style>
<template>
	<div class="page">
		<div ref="weixinTip" class="weixinTip" v-if="isWeixinFlag">
			<p>
				<image src="@/common/images/downApp/live_weixin.png" alt="微信打开" />
			</p>
		</div>
		<!-- :style="'height:' + docmHeight + 'px'" -->
		<div class="down-app-main">
			<div class="imgBox">
				<image class="img" :src="require('@/common/images/downApp/img.png')" />
				<image class="img2" :src="require('@/common/images/downApp/content.png')" />
			</div>
			<div class="down-app-main-content">
				<div class="down-app-main-img"><img :src="qrCodeUrl" /></div>

				<div class="down-app-main-btn">
					<div class="down-app-main-btn-left">
						<view class="c-btn" @click="handleAndriodClick">
							<image class="icon" :src="require('@/common/images/downApp/andriod.png')" mode=""></image>
							Andriod下载
						</view>
					</div>
					<div class="down-app-main-btn-left">
						<view class="c-btn" @click="iosDown">
							<image class="icon" :src="require('@/common/images/downApp/ios.png')" mode=""></image>
							iPhone下载
						</view>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	import {
		appDownloadUrl,
		getSysParam,
		centerDept
	} from '_a/sms';
	import appConfig from '@/mixins/appConfig'
	import QRCode from 'qrcode';

	export default {
		name: 'DownApp',
		mixins: [appConfig],
		data() {
			return {
				apkFile: '',
				qrCodeUrl: '',
				isWeixinFlag: false
			};
		},
		computed: {
			appConfig() {
				return this.$store.getters.appConfig || null
			}
		},


		mounted() {
			// #ifdef MP-WEIXIN
			this.isWeixin();
			// #endif
		},
		methods: {
			// 获取配置完后,加载页面逻辑
			loadData() {
				console.log('执行1111')
				// this.downApp();
				// console.log(this.$phoneAgent);
				this.getCode();
				this.getAppAndroidUrl();
			},
			getCode() {
				// #ifdef H5
				let message = window.location.href;
				// #endif
				
				// #ifdef APP-PLUS
				console.log('配置')
				console.log(this.appConfig)
				let message = this.appConfig.androidPath
				// #endif
				
				QRCode.toDataURL(message, {
						margin: 1
					})
					.then(url => {
						//    console.log('qrcode',url)
						this.qrCodeUrl = url;
					})
					.catch(err => {
						console.error(err);
					});
			},
			androidBack() {
				window.location.href = this.apkFile;
			},
			async getAppAndroidUrl() {
				// let deptInfo = await centerDept();
				// if(deptInfo.data?.deptId){
				// 	let appAndroidUrlRes = await getSysParam(`${deptInfo.data.deptId}:appAndroidUrl`);
				// 	if(appAndroidUrlRes.msg){
				// 		this.apkFile = appAndroidUrlRes.msg;
				// 	} else {
				// 		this.$tip.toast('请联系管理员上传app!');
				// 	}
				// }
				let res = {};
				
				// #ifdef H5
				let hostname = window.location.hostname
				// #endif
				
				// #ifdef APP-PLUS
				let hostname = this.appConfig.androidPath
				// #endif
				
				console.log(hostname);
				if (hostname == 'app.shou-kuanbao.com') {
					// 系统app
					res = await getSysParam('appAndroidUrl')
				} else {
					if(this.appConfig && this.appConfig.androidPath) {
						res.msg = this.appConfig.androidPath || ''
					}else{
						res.msg = 'download/newapp/agent_app.apk';
					}
					// res = await getSysParam('appAndroidUrl2')
				}
				if (res.msg) {
					this.apkFile = res.msg;
				} else {
					this.$tip.toast('请联系管理员上传app!');
				}
			},
			async downApp() {
				const userAgent = navigator.userAgent.toLowerCase();
				console.log(userAgent);
				if (/android|adr|linux/gi.test(userAgent)) {
					let params = {
						appOperSys: 0 //APP系统：0-安卓 , 1-苹果
					};

					appDownloadUrl(params).then(res => {
						if (res.code === 200 && res.data && res.data.versions) {
							this.apkFile = res.data.appUrl;
							console.log('this.apkFile', this.apkFile);
						} else {
							this.$tip.toast('请联系管理员上传app!');
						}
						// url: 'https://apps.apple.com/cn/app/id1532381936',
					});
				}
			},
			iosDown() {
				this.$router.push({
					path: '/pages/home/invitation/downIos'
				});
				// window.location.href = "itms-apps://itunes.apple.com/app/id1532381936";
			},
			handleAndriodClick() {
				console.log(this.apkFile);
				const userAgent = navigator.userAgent.toLowerCase(); //获取UA信息
				if (userAgent.match(/MicroMessenger/i) == 'micromessenger') {
					// 判断是否为微信内置浏览器
					this.$toast({
						duration: 1000,
						message: '微信浏览器暂时不支持下载安卓包'
					});
				} else {
					// alert('下载链接',this.apkFile)
					// console.log('下载链接',this.apkFile)
					if (this.apkFile) {
						// window.location.href = this.apkFile;
						// 构建新的URL：新域名 + /agent + apkFile
						const newDomain = "https://file.shou-kuanbao.com"; // 替换为你的新域名
						const downloadUrl = `${newDomain}/agent/${this.apkFile}`;
						window.location.href = downloadUrl;
					} else {
						const downloadUrl = "https://file.shou-kuanbao.com/agent/download/newapp/agent_app.apk";
						// this.$tip.toast('请联系管理员上传app!');
					}
				}
			},
			isWeixin() {
				const ua = navigator.userAgent.toLowerCase();
				if (ua.match(/MicroMessenger/i) == 'micromessenger') {
					this.isWeixinFlag = true;
				}
			}
		}
	};
</script>